<template>
    <view :style="objColor">
        <view class="nav-header" >
            <view class="item" @tap="switchIndex(0)" :class="tabIndex === 0 && 'active'">活动</view>
            <view class="item" @tap="switchIndex(1)" :class="tabIndex === 1 && 'active'">资讯</view>
        </view>

        <view :style="'padding-top:'+(statusBarHeight+30)+'px;'" v-show="tabIndex === 1">
            <view class="head">
                <view class="head-tab">
                    <view class="txt">{{ categoryName }}</view>
                    <image class="head-image" src="https://oos-cn.ctyunapi.cn/front-end/static/static/screen.png" mode="aspectFit" @tap="select"></image>
                </view>
                <view v-if="activityList.length">
                    <view class="activity" :data-id="item.id" @tap="naviDetail" v-for="(item, index) in activityList" :key="index" data-type="active">
                        <image class="img" :src="item.imgPath"></image>

                        <view class="info">
                            <view class="title">{{ item.title }}</view>
                            <view class="tagTxt">
                                <view class="tag">{{ item.findClassName }}</view>
                            </view>
                            <view class="detail">
                                <view v-if="item.readGuide">
                                    {{ item.readGuide }}
                                </view>
                                <!--<rich-text :nodes="item.content" v-else></rich-text>-->
                            </view>

                            <view class="fotData">{{ item.publishTime }} | {{ item.readCount }}人阅读</view>
                        </view>
                    </view>
                </view>
                <Upgrading text="暂无数据" v-if="activityList.length == 0 && loading"></Upgrading>
            </view>
        </view>

        <view :style="'padding-top:'+(statusBarHeight+30)+'px;'" v-show="tabIndex === 0">
            <view class="list-box">
                <view class="item-box" v-for="(item,index) in activityList2" :key="index"  :data-mobilepath="item.mobilePath" :data-id="item.id" :data-link="item.link" @tap="naviDetailNew" :data-connectiontype="item.connectionType" data-type="active2">
                    <image class="img" :src="item.coverImg"></image>
                    <image class="select" src="https://oos-cn.ctyunapi.cn/front-end/static/static/label.png" v-if="!item.readNumber"></image>
                    <view class="content">{{item.title}}</view>
                    <view class="time">{{(item.startTime.split(' ')[0]).replace(/-/g,".")}}-{{(item.endTime.split(' ')[0]).replace(/-/g,".")}}</view>
                </view>
            </view>
            <Upgrading text="暂无数据" v-if="activityList2.length == 0 && loading"></Upgrading>
        </view>
        <!-- <loding :loading="loading" :noMore="noMore" /> -->
        <u-popup :show="categoryStatus" position="bottom" @close="onClose" custom-style="border-radius:16rpx;width:702rpx;margin:0 24rpx 22rpx 24rpx;" class="coupons2-popup">
            <view class="filtrate">
                <view>
                    <view class="category_title">
                        <view>活动分类</view>
                    </view>
                    <view class="category_body">
                        <view class="categoryList_wrap">
                            <view class="categoryList" :data-index="index" @tap="naviCategory" v-for="(item, index) in categoryList" :key="index">
                                <view :class="'category-list-item ' + (currentIndex == index ? 'active' : '')">{{ item.name }}</view>
                            </view>
                        </view>
                        <view class="categoryItemList_wrap">
                            <view class="categoryItemList" @tap="clickItem" :data-index="index" v-for="(item, index) in categoryItemList" :key="index">
                                <view :class="'categoryItemList-item ' + (currentItemIndex == index ? 'active' : 'noactive')">{{ item.name }}</view>

                                <image src="https://oos-cn.ctyunapi.cn/front-end/static/static/itemSelect.png" class="itemSelect" v-if="currentItemIndex == index"></image>
                            </view>
                        </view>
                    </view>
                </view>
                <view class="filtrate-bottom">
                    <view class="filtrate-cancel" @tap="onClose">取消</view>
                    <view class="filtrate-confirm" @tap="onDetermine">确定</view>
                </view>
            </view>
        </u-popup>
        <!-- 下端导航栏 -->
        <Tabbar :currentPage="3" :isNew="isNew"></Tabbar>
    </view>
</template>

<script>
	'use strict';
	import Tabbar from '../../components/tabbar/tabbar';
	import Upgrading from '../../components/upgrading/upgrading';
	import loding from '../../components/loading/loading';

  const app = getApp();
	var find_1 = require('../../api/find');
	var https_1 = require('../../api/https.js');
	export default {
		components: {
			Tabbar,
			Upgrading,
			loding
		},
		data() {
			return {
				loading: false,
				noMore: false,
				activityList: [],
				activityList2: [],
				categoryName: '全部',
				tabIndex:0,
				activityListConfig: {
					current: 1,
					pageSize: 10,
					findClassId: '',
					publishType: '',
					title: '',
					sellerId:app.globalData.sellerId,
				},
				isNew:false,
				statusBarHeight:uni.getSystemInfoSync().statusBarHeight,
				activityListConfig2: {
					current: 1,
					pageSize: 10,
          sellerId:app.globalData.sellerId,
				},

				categoryStatus: false,
				currentIndex: '0',
				currentItemIndex: '0',
				categoryItemList: [],

				categoryList: [
					{
						name: '分类1',
						childrenClass: [
							{
								name: '分类1 - 1'
							},
							{
								name: '分类1 - 2'
							},
							{
								name: '分类1 - 3'
							},
							{
								name: '分类1 - 4'
							},
							{
								name: '分类1 - 5'
							}
						]
					},
					{
						name: '分类2',
						childrenClass: [
							{
								name: '分类2 - 1'
							},
							{
								name: '分类2 - 2'
							},
							{
								name: '分类2 - 3'
							},
							{
								name: '分类2 - 4'
							},
							{
								name: '分类2 - 5'
							}
						]
					}
				],
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function () {

		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function () {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function () {
			uni.hideTabBar()
			// if(uni.getStorageSync('token')){
				this.findClassPage();
				this.resetConfig();
				this.resetConfig2();
				this.getActivityList(1);
				this.getActivityList2(1);
				https_1.getNew().then((res) => {
					if (res.code == 0) {
						this.isNew = res.data
					}
				})
			// }
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function () {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function () {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function () {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function () {
			if(this.tabIndex === 1){
				this.getActivityList();
			}else {
				this.getActivityList2()
			}
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function () {},
		methods: {

			// 显示全部
			showAll: function () {
				this.setData({
					categoryStatus: false,
					categoryName: '全部'
				});
				this.resetConfig();
				this.getActivityList(1);
			},
			switchIndex(index){
				this.tabIndex = index
				// if(uni.getStorageSync('token')){
					if(index === 1){
						this.resetConfig();
						this.getActivityList();
					}else {
						this.resetConfig2();
						this.getActivityList2()
					}
				// }
			},

			// 重置请求参数
			resetConfig: function () {
				this.activityListConfig.current = 1;
				this.activityListConfig.findClassId = '';
				this.activityListConfig.publishType = '';
				this.activityListConfig.title = '';
				this.activityListConfig.sellerId = app.globalData.sellerId;
			},
			// 重置请求参数
			resetConfig2: function () {
				this.activityListConfig2.current = 1;
			},

			// 获取分类名称
			// getCategoryName: function () {
			//     var _a = this.data, currentIndex = _a.currentIndex, currentItemIndex = _a.currentItemIndex, categoryList = _a.categoryList;
			//     var categoryName;
			//     var firstName = categoryList[currentIndex] ? .name
			//         :
			//     ;
			//     var lastName = categoryList[currentIndex] ? .childrenClass[currentItemIndex] ? .name
			//         :
			//         :
			//     ;
			//     categoryName = firstName + (lastName ? " - " + lastName : '');
			//     return categoryName;
			// },
			// 获取分类
			findClassPage: function () {
				var that = this;
				find_1.findClassPage({ sellerId:app.globalData.sellerId }).then(function (res) {
					if (res.code == 0) {
						var categoryList = res.data.records || [];
						that.setData({
							categoryList: categoryList,
							categoryItemList: categoryList.length ? categoryList[0].childrenClass : []
						});
					}
				});
			},

			// 获取发现里列表
			getActivityList: function (type) {
				console.log("进入getActivityList方法")
				var that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				that.loading=false
				if(type !== 1){
					that.activityListConfig.current++;
				}
				var activityListConfig = that.activityListConfig;
				find_1.findPage(activityListConfig).then(function (res) {
					if (res.code == 0) {
						that.setData({
							activityList: type === 1?res.data.records : that.activityList.concat(res.data.records),
							noMore: res.data.records.length == 0,
							loading: true
						});
						console.log("打印activityList数据",that.activityList)
						uni.hideLoading();
					}
				});
			},
      // 获取发现里列表
			getActivityList2: function (type) {
				console.log("进入getActivityList方法",type)
				var that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
						that.loading=false
				if(type !== 1){
					that.activityListConfig2.current++;
				}
				var activityListConfig = that.activityListConfig2;
				find_1.miniPageQuery(activityListConfig).then(function (res) {
					if (res.code == 0) {
						that.setData({
							activityList2: type === 1?res.data.records : that.activityList2.concat(res.data.records),
							noMore: res.data.records.length == 0,
							loading: true
						});
						console.log("打印activityList2数据",that.activityList2)
						uni.hideLoading();
					}
				});
			},

			select: function () {
				//   console.log(121212)
				this.setData({
					categoryStatus: true
				});
			},

			onClose: function () {
				this.setData({
					categoryStatus: false
				});
			},

			// naviCategory: function (e) {
			//     var index = e.currentTarget.dataset.index;
			//     // // 因为添加了全部所以要index 要-1
			//     // index = index - 1
			//     var categoryList = this.data.categoryList;
			//     this.setData({
			//         currentIndex: index,
			//         categoryItemList: (categoryList[index] ? .childrenClass : ),
			//         currentItemIndex: (categoryList[index]) ? .itemIndex ?  ? '0'
			//             :
			//             :
			//             :
			//     });
			// },
			clickItem: function (e) {
				var index = e.currentTarget.dataset.index;
				var _a = this;
				var currentIndex = _a.currentIndex;
				var categoryList = _a.categoryList;
				categoryList[currentIndex].itemIndex = index;
				this.setData({
					currentItemIndex: index
				});
			},

			naviDetail: function (e) {
				console.log(e,'eeeeee11')
				if(e.currentTarget.dataset.mobilepath){
					uni.navigateTo({
						url: e.currentTarget.dataset.mobilepath+'?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
					});
					return false
        }
        if(e.currentTarget.dataset.link){
					uni.navigateTo({
						url: '/components/webView/webView?link=' + e.currentTarget.dataset.link+'&id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
					});
					return false
        }
				uni.navigateTo({
					url: '../../homeSub/pages/findDetail/findDetail?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
				});
			},
			//活动跳转
			naviDetailNew: function (e) {
				console.log(e.currentTarget.dataset)

				var token = uni.getStorageSync('token');
				if(token){
					this.naviDetail(e);
				}else{
					var types = e.currentTarget.dataset.connectiontype; // 1是详情   2跳转
					console.log(types)

					switch (types) {
						case 1:
							if(e.currentTarget.dataset.mobilepath){
								uni.navigateTo({
									url: e.currentTarget.dataset.mobilepath+'?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
								});
								return false
							}
							if(e.currentTarget.dataset.link){
								uni.navigateTo({
									url: '/components/webView/webView?link=' + e.currentTarget.dataset.link+'&id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
								});
								return false
							}
							uni.navigateTo({
								url: '../../homeSub/pages/findDetail/findDetail?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
							});
							break;
						default:
							app.globalData.CHECK_LOGIN(()=>{
								return false;
							})
							break;
					}
				}
				
				
			},

			naviCategory(e) {
				var { currentTarget: { dataset: { index } } } = e
				// // 因为添加了全部所以要index 要-1
				// index = index - 1
				var categoryList = this.categoryList
				this.setData({
					currentIndex: index,
					categoryItemList: (categoryList[index].childrenClass),
					currentItemIndex: (categoryList[index]).itemIndex
				})

				console.log("打印naviCategory方法data",this.currentIndex,this.categoryItemList,this.currentItemIndex)
			},

			onDetermine() {
				console.log('占位：函数 onDetermine 未声明');
				this.resetConfig()
				var currentIndex = this.currentIndex
				var currentItemIndex = this.currentItemIndex
				var categoryList  = this.categoryList
				var findClassId = categoryList[currentIndex].childrenClass[currentItemIndex].id ?categoryList[currentIndex].childrenClass[currentItemIndex].id:categoryList[currentIndex].id
				console.log(currentIndex, currentItemIndex, categoryList)
				console.log(categoryList[currentIndex].id)
				console.log(categoryList[currentIndex].childrenClass[currentItemIndex]?.id)
				console.log(findClassId, 'findClassIdfindClassId')
				this.activityListConfig.findClassId = findClassId
				this.setData({
					// categoryName: this.getCategoryName(),
					categoryStatus: false,
					activityList: []
				})
				this.getActivityList(1)
			}
		}
	};
</script>
<style lang="less">
    @import url("./index.less");
</style>
